package shopping;


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        while (true) {
            Scanner scanner = new Scanner(System.in);

            Product[] products = new Product[3];
            Product p1 = new Product("苹果", "水果", 8, 0);
            Product p2 = new Product("西瓜汁", "饮品", 25, 0);
            Product p3 = new Product("可乐", "饮品", 3, 0);

            products[0] = p1;
            products[1] = p2;
            products[2] = p3;

            ShoppingCart cart = new ShoppingCart();
            cart.setCapacity(30);
            System.out.println("请输入您想进行的操作");
            String opt = scanner.nextLine();

            if (opt.equals("add")) {
                //用户自我选择商品，然后陈列出该类商品下的各个商品信息

                System.out.println("请输入所选商品的名称：");
                String name = scanner.nextLine();
                selectProduct(name, products);

                //提示顾客将商品加入购物车
                System.out.println("是否加入购物车(yes or no)");
                if ("yes".equals(scanner.nextLine())) {
                    System.out.println("请输入商品数量");
                    int quantity = scanner.nextInt();
                    addProduct(selectProduct(name, products), quantity, cart);
                }
            }


            else if (opt.equals("check")) {
                cart.check();
            }


            else if (opt.equals("delete")) {
                System.out.println("请输入你要删除的商品");
                String productName = scanner.nextLine();
                cart.deleteProduct(productName);

            }

            else if (opt.equals("close")) {
                cart.closeAccount();
            }

            else if (opt.equals("over")) {
                break;
            }


        }
    }


    public static Product selectProduct(String name, Product[] products) {
        //用户自我选择商品，然后陈列出该类商品下的各个商品信息
        Product product1 = null;
        //添加至购物车，可选择添加数量
        for (int i = 0; i < products.length; i++) {
            if (products[i].getName().equals(name)) {
                product1 = products[i];
                System.out.println("name = " + name + ", price = " + products[i].getPrice());

                break;
            } else {
                System.out.println("该商品信息不存在");
            }
        }
        return product1;
    }

    public static void addProduct(Product product, int quantity, ShoppingCart shoppingCart) {


        if (product.equals(shoppingCart.getProducts())) {
            System.out.println("请勿重复添加商品");
        } else {
            shoppingCart.addProduct(product, quantity);
        }

    }





}



